package com.gs.SynPatrolRep.dialog;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gs.SynPatrolRep.R;
import com.gs.SynPatrolRep.activity.ImagePreviewActivity;
import com.gs.SynPatrolRep.adapter.DialogAdapter;
import com.gs.SynPatrolRep.adapter.ReportResultPhotoAdapter;
import com.gs.SynPatrolRep.adapter.base.BaseAdapter;
import com.gs.SynPatrolRep.constant.AllConstants;
import com.gs.SynPatrolRep.db.dto.CreatePalBillEventResultDto;
import com.gs.SynPatrolRep.db.dto.PalBillEventDto;
import com.gs.SynPatrolRep.db.dto.ShortDto;
import com.gs.SynPatrolRep.util.DateUtil;
import com.gs.SynPatrolRep.util.OKHttpUtil;

import java.util.ArrayList;
import java.util.List;

public class ReportResultDialog extends BaseDialog {

    private Context context;
    private EditText  etResult;
    private TextView tvSum;
    private String billName;
    private List<PalBillEventDto.PalBillEventPhotoDto> photos;
    private ArrayList<String> listPhotos;

    public ReportResultDialog(@NonNull Context context, String billName, List<PalBillEventDto.PalBillEventPhotoDto> photos,IChoiceListener iChoiceListener) {
        super(context, R.style.toast_dialog);
        this.context = context;
        this.billName = billName;
        this.photos = photos;
        this.iChoiceListener = iChoiceListener;
    }

    @Override
    public void refreshView() {

    }

    @Override
    public void initView() {
        TextView tvCon = findViewById(R.id.tv_con);
        tvCon.setText(billName);
        etResult = findViewById(R.id.et_result);
        etResult.addTextChangedListener(new TextWatcher());
        tvSum = findViewById(R.id.tv_sum);
        OnClickListener onClickListener = new OnClickListener();
        findViewById(R.id.btn_close).setOnClickListener(onClickListener);
        findViewById(R.id.btn_no).setOnClickListener(onClickListener);
        findViewById(R.id.btn_processing).setOnClickListener(onClickListener);
        if (photos.isEmpty()) {
            findViewById(R.id.ll_photo).setVisibility(View.GONE);
            return;
        }
        RecyclerView recyclerView = findViewById(R.id.recyclerview);
        ReportResultPhotoAdapter adapter = new ReportResultPhotoAdapter(context,photos,R.layout.rvitem_photo);
        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View v, int position) {
                Intent intent = new Intent();
                intent.setClass(context, ImagePreviewActivity.class);
                if (listPhotos == null) {
                    listPhotos = new ArrayList<>();
                    for (int i = 0; i < photos.size(); i++) {
                        listPhotos.add(OKHttpUtil.IMG_URL + photos.get(i).getPhoto());
                    }
                }
                intent.putExtra(AllConstants.WHAT, listPhotos);
                intent.putExtra(AllConstants.ID, position);
                context.startActivity(intent);
            }
        });
        recyclerView.setLayoutManager(new GridLayoutManager(context, 3));
        recyclerView.setAdapter(adapter);
    }

    class TextWatcher implements android.text.TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            tvSum.setText(s.toString().length() + "/100");
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    class OnClickListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_close:
                    dismiss();
                    break;
                case R.id.btn_no:
                    CreatePalBillEventResultDto bean = new CreatePalBillEventResultDto();
                    bean.setPro(2);
                    iChoiceListener.getChoiceFromDialog(bean);
                    dismiss();
                    break;
                case R.id.btn_processing:
                    if (TextUtils.isEmpty(etResult.getText().toString())) {
                        ToastDialogUtils.getInstance().showInfoMessage(context, "請輸入處理結果");
                        return;
                    }
                    CreatePalBillEventResultDto dto = new CreatePalBillEventResultDto();
                    dto.setPro(1);
                    dto.setProResult(etResult.getText().toString());
                    iChoiceListener.getChoiceFromDialog(dto);
                    dismiss();
                    break;
            }
        }
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_report_result;
    }

    private IChoiceListener iChoiceListener;

    public interface IChoiceListener {
        void getChoiceFromDialog(CreatePalBillEventResultDto bean);
    }
}
